<?php
class RDFImport extends SpecialPage {

    protected $m_smwbatchwriter;
    protected $m_haswriteaccess;
    
    function __construct() {
        global $wgUser;

        $userrights = $wgUser->getRights();
        if ( in_array( 'edit', $userrights ) && in_array( 'createpage', $userrights ) ) {
            $this->m_haswriteaccess = true;
        } else {
            $this->m_haswriteaccess = false;
        } 
        parent::__construct( 'RDFImport' );
        wfLoadExtensionMessages('RDFImport');
    }

    function execute( $par ) {
        global $wgRequest, $wgOut, $wgUser, $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgEnableWriteAPI, $wgArticlePath;

        $this->setHeaders();
        # Get request data from
        $action = $wgRequest->getText('action');
        $dataformat = $wgRequest->getText('dataformat');
        $importdata = $wgRequest->getText('importdata');
        if ( $action == 'Import' ) {
            if ( !$wgUser->matchEditToken( $wgRequest->getText( 'token' ) ) ) {
                die( 'Cross-site request forgery detected!' );
            } else {
                if ( $this->m_haswriteaccess ) {
                    $this->m_smwbatchwriter = new RDFIOSMWBatchWriter( $importdata, $dataformat );
                    $this->m_smwbatchwriter->execute();
                } else {
                    $errortitle = "Permission error";
                    $errormessage = "The current user lacks access either to edit or create pages (or both) in this wiki.";
                    $wgOut->addHTML( RDFIOUtils::formatErrorHTML( $errortitle, $errormessage ) );
                }
            }
        }

        // Create the HTML form for RDF/XML Import
        $htmlFormContent = '<form method="post" action="' . str_replace('/$1', '', $wgArticlePath) . '/Special:RDFImport"
            name="createEditQuery">
            <table border="0"><tbody>
            <tr><td colspan="2">RDF/XML data to import:</td><tr>
            <tr><td colspan="2"><textarea cols="80" rows="12" name="importdata">' . $this->getrdfToPost() . '</textarea></td></tr>
            <tr><td width="80">Data format:</td>
            <td>
            <select id="dataformat" name="dataformat"> 
              <option value="rdfxml" selected="selected">RDF/XML</option> 
              <option value="turtle" >Turtle</option> 
            </select>
            </td></tr>
            </tbody></table>
            <input type="submit" name="action" value="Import">' . Xml::hidden( 'token', $wgUser->editToken() ) . '
            </form>';
        $wgOut->addHTML( $htmlFormContent );
        # $wgOut->addWikiText( $output );
    }

    /**
     * Get RDF/XML stub for for the import form, including namespace definitions
     * @return string
     */
    public function getRDFToPost() {

        $old_return = '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:nmr="http://www.nmrshiftdb.org/onto#"
         xmlns:sid="http://pele.farmbio.uu.se/nmrshiftdb/?spectrumId="
         xmlns:mid="http://pele.farmbio.uu.se/nmrshiftdb/?moleculeId="
         xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
         xmlns:chem="http://www.blueobelisk.org/chemistryblogs/"
         xmlns:dc="http://purl.org/dc/elements/1.1/"
         xmlns:foaf="http://xmlns.com/foaf/0.1/"
         xmlns:owl="http://www.w3.org/2002/07/owl#"
         xmlns:bibo="http://purl.org/ontology/bibo/">

         

         </rdf:RDF>';
        
        return '<rdf:RDF
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:n0pred="http://bio2rdf.org/go_resource:"
	xmlns:ns0pred="http://www.w3.org/2002/07/owl#">

<rdf:Description rdf:about="http://bio2rdf.org/go:0032283">
	<n0pred:accession>GO:0032283</n0pred:accession>
	<rdfs:label>plastid acetate CoA-transferase complex [go:0032283]</rdfs:label>
	<n0pred:definition>An acetate CoA-transferase complex located in the stroma of a plastid.</n0pred:definition>
	<rdf:type rdf:resource="http://bio2rdf.org/go_resource:term"/>
	<n0pred:name>plastid acetate CoA-transferase complex</n0pred:name>
	<n0pred:is_a rdf:resource="http://bio2rdf.org/go:0009329"/>
	<rdf:type rdf:resource="http://bio2rdf.org/go_resource:Term"/>
	<urlImage xmlns="http://bio2rdf.org/bio2rdf_resource:">http://bio2rdf.org/image/go:0032283</urlImage>
	<xmlUrl xmlns="http://bio2rdf.org/bio2rdf_resource:">http://bio2rdf.org/xml/go:0032283</xmlUrl>
	<rights xmlns="http://purl.org/dc/terms/" rdf:resource="http://www.geneontology.org/GO.cite.shtml"/>
	<ns0pred:sameAs rdf:resource="http://purl.org/obo/owl/GO#GO_0032283"/>
	<url xmlns="http://bio2rdf.org/bio2rdf_resource:">http://bio2rdf.org/html/go:0032283</url>
</rdf:Description>

</rdf:RDF>';
    }

}
